﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using cancerportal.Models;
using cancerportal.Entitys;
using System.IO;
using Microsoft.Xml.XQuery;
using System.Xml;
namespace cancerportal.Controllers
{
    public class clientMessageController : Controller
    {
        public int currentSymptomAsk { get; set; }
        public AskSymptomModels robot {set; get;}
        public List<string> xmlData;
        public List<string> listSymptom {get; set;}
        public List<int> listSymptomID { get; set; }
        public List<string> listOtherWord { get; set; }
        public string symptomAllReturn { get; set; }
        public clientMessageController()
        {
            this.currentSymptomAsk = 0;
            this.robot = new AskSymptomModels();
        }

        public ActionResult Index()
        {
            return View();
        }

        public string messageReply(string msg) 
        {

            String suggested = "";
            int old = -1;
            if (Session["start"] == null)
            {
                this.robot.listRank(msg);
                int listlength = this.robot.allSymptom.Count;
                Session["start"] = true;
                if (listlength != 0)
                {
                    Session["length"] = listlength;
                    Session["currentSuggested"] = 0;
                    suggested = this.robot.allSymptom[0].symptom_name;
                    Session["robot"] = this.robot;
                    return "Bạn có bị " + suggested + "?";
                }
                else
                {
                    return "Ý bạn là ??";
                }
            }
            else if ((int)Session["length"] != 0 && (int)Session["currentSuggested"] < (((int)Session["length"]) - 1))
            {
                AskSymptomModels robotSession = (AskSymptomModels)Session["robot"];
                old = (int)Session["currentSuggested"];
                if (msg == "yes")
                {
                    robotSession.UpdateListRank(robotSession.allSymptom[old].symptom_id);
                    Session["robot"] = robotSession;
                }
                int current = old + 1;
                Session["currentSuggested"] = current;
                suggested = robotSession.allSymptom[current].symptom_name;
                return "Do you have " + suggested + "?";
            }
            AskSymptomModels robotend = (AskSymptomModels)Session["robot"]; //when all symptom is done!
            if (msg == "yes")
            {
                old = (int)Session["currentSuggested"];
                robotend.UpdateListRank(robotend.allSymptom[old].symptom_id);
            }
            String result = "";
            foreach (disease obj in robotend.listDisease.ToList())
            {
                result += obj.id + " : " + obj.currentWeight + " ; ";
            }
            Session.Clear();
            return result;
        }

        public string ngramAlgorithms(string msg)
        {
            float lamda1 = 0.991f;
            float lamda2 = 0.009f;
            string content = "";
            int i = 0;
            List<string> listWord = new List<string>();
            listOtherWord = new List<string>();
            listSymptom = new List<string>();
            listSymptomID = new List<int>();
            List<string> listSentence = new List<string>();
            string[] listsentencetemp = msg.Split('.');
            for (i = 0; i<listsentencetemp.Length ; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    string pre = j.ToString();
                    if (listsentencetemp[i].EndsWith(pre))
                    {
                        for (int k = 0; k < 10; k++)
                        {
                            string end = k.ToString();
                            if (listsentencetemp[i + 1].StartsWith(end))
                            {
                                listsentencetemp[i] = listsentencetemp[i] + "." +listsentencetemp[i + 1];
                                for (int h = i+1; h < listsentencetemp.Length - 1; h++)
                                {
                                    listsentencetemp[h] = listsentencetemp[h + 1];
                                }
                            }
                        }
                    }
                }
            }
            List<string> listSentenceTemp = listsentencetemp.ToList();
            foreach (string str in listSentenceTemp)
            {
                if (!str.Equals(""))
                {
                    listSentence.Add(str.TrimStart());
                }
            }
            foreach (string sen in listSentence)
            {
                string[] tempArr = sen.Split(' ');
                for (int f = 0; f< tempArr.Length; f++)
                {
                    char charEndTemp = tempArr[f][tempArr[f].Length - 1];
                    char charStartTemp = tempArr[f][0];
                    string endchar = charEndTemp.ToString();
                    string startchar = charStartTemp.ToString();
                    if (tempArr[f].EndsWith(endchar) && (endchar.Equals(",") || endchar.Equals("/") || endchar.Equals("\\") || endchar.Equals(";") || endchar.Equals("-") || endchar.Equals("_") || endchar.Equals("(") || endchar.Equals(")") || endchar.Equals("@") || endchar.Equals("$") || endchar.Equals("*") || endchar.Equals("?") || endchar.Equals("&") || endchar.Equals("~") || endchar.Equals("=") || endchar.Equals("+") || endchar.Equals("|") || endchar.Equals("}") || endchar.Equals("{") || endchar.Equals("[") || endchar.Equals("]")))
                    {
                        char[] arrChar = endchar.ToCharArray();
                        string[] xyz = tempArr[f].Split(arrChar[0]);
                        tempArr[f] = xyz[0];
                    }
                    if (tempArr[f].StartsWith(startchar) && (startchar.Equals(",") || startchar.Equals("/") || startchar.Equals("\\") || startchar.Equals(";") || startchar.Equals("-") || startchar.Equals("_") || startchar.Equals("(") || startchar.Equals(")") || startchar.Equals("@") || startchar.Equals("$") || startchar.Equals("*") || startchar.Equals("?") || startchar.Equals("&") || startchar.Equals("+") || startchar.Equals("=") || startchar.Equals("~") || startchar.Equals("|") || startchar.Equals("}") || startchar.Equals("{") || startchar.Equals("]") || startchar.Equals("[") || startchar.Equals("~")))
                    {
                        char[] arrChar = startchar.ToCharArray();
                        string[] xyz = tempArr[f].Split(arrChar[0]);
                        tempArr[f] = xyz[0];
                    }
                }
                foreach (string str in tempArr)
                {
                    listWord.Add(str);
                }
            }
            string[] arrWordTemp = listWord.ToArray();
            listWord = new List<string>(0);
            List<string> listWordremove = new List<string>();
            int length = arrWordTemp.Length;
            for (int p = 0; p < length - 1; p++)
            {
                string wordTemp = arrWordTemp[p];
                bool test = false;
                if ((length - p) >= 5)
                {
                    int dem = 0;
                    test = false;
                    for (int q = p + 1; q < p + 5; q++)
                    {
                        wordTemp = wordTemp + " " + arrWordTemp[q];
                        wordTemp.ToLower();
                        if (checkExistsSymptom(wordTemp))
                        {
                            listSymptom.Add(wordTemp);
                        }
                        else
                        {
                            if (checkExistsWordInDictionary(wordTemp) && !(checkExistsNormalWord(wordTemp)))
                            {
                                dem = dem = 1;
                                listWord.Add(wordTemp);
                                listWordremove.Add(arrWordTemp[q]);
                                listOtherWord.Add(wordTemp);
                            }
                            else
                            {
                                test = true;
                            }
                        }
                    }
                    if (test && (dem == 0))
                    {
                        if (checkExistsWordInDictionary(arrWordTemp[p]) && !(checkExistsNormalWord(arrWordTemp[p])))
                        {
                            listWord.Add(arrWordTemp[p]);
                            listOtherWord.Add(arrWordTemp[p]);
                        }
                    }
                }
                else
                {
                    int dem = 0;
                    test = false;
                    for (int q = p + 1; q < length; q++)
                    {
                        wordTemp = wordTemp + " " + arrWordTemp[q];
                        if (checkExistsSymptom(wordTemp))
                        {
                            listSymptom.Add(wordTemp);
                        }
                        else
                        {
                            if (checkExistsWordInDictionary(wordTemp) && !(checkExistsNormalWord(wordTemp)))
                            {
                                dem = dem + 1;
                                listWord.Add(wordTemp);
                                listWordremove.Add(arrWordTemp[q]);
                                listOtherWord.Add(wordTemp);
                            }
                            else
                            {
                                test = true;
                            }
                        }
                    }
                    if (test && (dem == 0))
                    {
                        if (checkExistsWordInDictionary(arrWordTemp[p]) && !(checkExistsNormalWord(arrWordTemp[p])))
                        {
                            listWord.Add(arrWordTemp[p]);
                            if ((p == length - 2) && checkExistsWordInDictionary(arrWordTemp[length - 1]) && !(checkExistsNormalWord(arrWordTemp[length - 1])))
                            {
                                listWord.Add(arrWordTemp[length - 1]);
                            }
                            listOtherWord.Add(arrWordTemp[p]);
                        }
                    }
                }
                symptomAllReturn = "<Symptom><id>";
                foreach (string st in listSymptom)
                {
                    symptomAllReturn = symptomAllReturn + "<id>" + Models.symptomModels.getSymptomByName(st).symptom_id.ToString() +"</id>";
                }
                symptomAllReturn = symptomAllReturn + "</Symptom>";
            }
            var returnMsg = this.messageReply(symptomAllReturn);
            return returnMsg;
        }

        public bool checkExistsSymptom(string msg)
        {
            bool isSymptom = false;
            isSymptom = Models.symptomModels.checkExistSymptom(msg);
            return isSymptom;
        }

        public bool checkExistsNormalWord(string msg)
        {
            bool isNormal = false;
            isNormal = Models.normalWordModels.checkExistWord(msg);
            return isNormal;
        }

        public bool checkExistsWordInDictionary(string msg)
        {
            bool isWord = false;
            isWord = Models.dictionaryModels.checkExistWord(msg);
            return isWord;
        }

        
        public float proTwoWord(string msg)
        {
            string word1; 
            string word2;
            string[] arr = msg.Split(',');
            word1 = arr[0];
            word2 = arr[1];
            float pro = 0;
            int count1 = Models.corpusModels.countNumberWord(word1);
            int count2 = Models.corpusModels.countNumberWord(word2);
            pro = count1 / count2;
            return pro;
        }
    }
}
